package com.taotao.job.rpc.server;

import com.google.common.base.Preconditions;
import com.taotao.job.rpc.core.meta.TaotaoRpcException;
import com.taotao.job.rpc.core.utils.ReflectUtil;
import com.taotao.job.rpc.server.hold.ProviderHolder;

import java.lang.reflect.Method;

/**
 * @author fanbeibei
 * @date 2021/4/5 13:59
 */
public abstract class AbstractRpcProviderServer implements RpcProviderServer {

    protected ProviderHolder providerHolder = ProviderHolder.getInstance();

    @Override
    public void addProviderService(Object providerService, Class<?> ifaceClass) {
        Preconditions.checkArgument(null != providerService);

        Class<?> rpcServiceClass = providerService.getClass();

        //判断是否继承ifaceClass
        if (!ReflectUtil.isImplementsFor(providerService, ifaceClass)) {
            throw new TaotaoRpcException("providerService " + rpcServiceClass + " does not implements  " + ifaceClass);
        }

        Method[] methods = ifaceClass.getMethods();
        for (Method m : methods) {
            providerHolder.registerProvider(providerService, ifaceClass, m);
        }
    }
}
